package westh.ilib.struts.action.researcher;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.DynaActionForm;
import org.mule.extras.client.MuleClient;
import org.mule.umo.UMOMessage;

import westh.config.Config;
import westh.ilib.bean.researcher.Researcher;

public class SearchResearcherAction extends Action{
	private final static Log log = LogFactory.getLog(SearchResearcherAction.class);
	//get clouds Ip address
	String localhostUrl = Config.get("localhostUrl");
	String remoteUrl = Config.get("remoteUrl");
	
	public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) {
		DynaActionForm f = (DynaActionForm) form;
		String inputString = f.getString("researcherName");
		JSONObject inputObj = new JSONObject();
		inputObj.put("researcherName", inputString);
		String inputStr = inputObj.toString();
		log.debug("in SearchRsAction input =" + inputString);
		
		//jSon-String
		String outString = "";
		/**************获取jSon信息 start***************************/		
		try {
			MuleClient client = new MuleClient();
			//调用WebService
			UMOMessage umo = client.send("axis:http://"+localhostUrl+":4381/LIBJson/iDlibJson?method=getResearcherList",inputStr,null);
			outString = (String)umo.getPayload();
			log.debug("outString="+outString);
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
		/**************获取jSon信息 end***************************/
		String retCode=""; //返回代码
		String retMsg="";//返回结果信息
		String returnStr="";//跳转信息
		/****************解析输出jSon串   start*********************/
		JSONObject jsonOut= JSONObject.fromObject(outString);
		retCode = jsonOut.getString("retCode");
		retMsg = jsonOut.getString("retMsg");
		
		if (retCode.equals("1200")) {
			String result = jsonOut.getString("result");
			log.debug("result =" + result);
			JSONArray resultArry = JSONArray.fromObject(result);
			log.info("JSONArray=" + resultArry.size());
			
			List list = new ArrayList();
			
			for(int i = 0; i < resultArry.size(); i++){
				JSONObject jsonOut1 = resultArry.getJSONObject(i);		
				log.debug("jsonOut1 =" + jsonOut1);
				Researcher  researcher = new Researcher();
				researcher.setAuthorId(jsonOut1.getString("researcherId"));
				researcher.setResName(jsonOut1.getString("researcherName"));
				researcher.setResInstitute(jsonOut1.getString("researcherInstitute"));
				list.add(researcher);
			}
			
			/****************解析输出jSon串   end*********************/
			request.setAttribute("list", list);
			return mapping.findForward("succ");
		}
		else if (retCode.equals("1201")) {
			retMsg = jsonOut.getString("retMsg");
			request.setAttribute("info", retMsg);
			return mapping.findForward("fail");
		}
		
		return mapping.findForward("fail");
	}
}
